﻿using AutoMapper;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using Tools.Core.Dtos;
using Tools.Core.Entities;
using Tools.Services.Interfaces;

namespace Tools.Modules.Dialogs.ViewModels
{
    public class MailMessageViewModel : BindableBase,IDialogAware
    {
        private readonly IMailMessageService _mms;
        private readonly IMapper _mapper;
        private MailMessageDto _message=new MailMessageDto();
        public MailMessageDto Message
        {
            get => _message;
            set=>SetProperty(ref _message, value);
        }
        public DelegateCommand SubmitMessageCommand { get; private set; }
        public MailMessageViewModel(IMailMessageService mms,IMapper mapper)
        {
            _mms = mms;
            _mapper = mapper;
            SubmitMessageCommand = new DelegateCommand(async() =>
            {
                var m = _mapper.Map<MailMessage>(Message);
                await _mms.UpdateMessageAsync(m);
            });
        }

        public string Title => "邮件内容";

        public event Action<IDialogResult>  RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            RequestClose?.Invoke(null);
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            var message = parameters.GetValue<MailMessageDto>("message");
            if (message != null)
            {
                Message = message;
            }
        }
    }
}
